/**
 * 
 */
package com.smarthome.web.property.controller.system;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.core.propertystaffmanager.PropertyCompanyService;
import com.smarthome.core.user.PropertyEmployeeService;
import com.smarthome.core.user.PropertyUserService;
import com.smarthome.domain.bean.ShPropertyCompany;
import com.smarthome.domain.bean.ShPropertyEmployee;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.enums.CredentialType;
import com.smarthome.domain.enums.SexEnum;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.web.property.utils.UserHolder;

/**
 * @author dhc
 * @date 2015年9月26日 下午4:43:10
 * @title 物业人员管理类（系统人员管理类）
 */
@Controller
@RequestMapping("/propertyEmployee")
public class PropertyEmployeeComtroller {
		private Logger logger = LoggerFactory.getLogger(PropertyEmployeeComtroller.class);
	
		@Resource(name="propertyEmployeeService")
		private PropertyEmployeeService employeeService;  
		
		@Resource(name="propertyCompanyService")
		private PropertyCompanyService companyService;
		
		@Resource(name="propertyUserService")
		private PropertyUserService propertyUserService;
		
	    @RequestMapping("/views")
	    public String views(HttpServletRequest request){
	    	boolean isAdmin = UserHolder.isSuperResidence();
	    	request.setAttribute("isAdmin", isAdmin);
	    	ShPropertyCompany company = new ShPropertyCompany();
	    	if(!isAdmin){
	    		company.setResidenceId(UserHolder.getCurrentResidenceId());
	    	}
	    	request.setAttribute("companey",companyService.getComboboxs(company));
	    	return "/system/propertystaffmanager/propertyEmplyee"; 
	    }
	    
	    /**
	     * @title 查询 datagrid
	     * @createTime 2015年9月26日 下午5:02:45
	     * @author dhc
	     */
	    @RequestMapping("/dataGrid")
	    @ResponseBody
	    public DataGrid datagrid(ShPropertyEmployee employee,PageHelper page){
	    	if(!UserHolder.isSuperResidence()){
	    		if(null == employee) employee = new ShPropertyEmployee();
	    		employee.setResidenceId(UserHolder.getCurrentResidenceId());
	    	}
	    	
	    	return employeeService.datagrid(employee, page);
	    }
	    
	    @RequestMapping("addPage")
	    public String addPage(HttpServletRequest request){
	    	ShPropertyCompany company = new ShPropertyCompany();
	    	if(!UserHolder.isSuperResidence()){
	    		company.setResidenceId(UserHolder.getCurrentResidenceId());
	    	}
	    	request.setAttribute("companey",companyService.getComboboxs(company));
	    	//性别
	    	request.setAttribute("sexType", SexEnum.values());
	    	//证件种类
	    	request.setAttribute("credentialType", CredentialType.values());
	    	
	    	return "/system/propertystaffmanager/addPropertyEmployee"; 
	    }
	    @RequestMapping("editPage")
	    public String eidtPage(HttpServletRequest request,Long id){
	    	request.setAttribute("employee", employeeService.queryPropertyEmployeeById(id));
	    	ShPropertyCompany company = new ShPropertyCompany();
	    	if(!UserHolder.isSuperResidence()){
	    		company.setResidenceId(UserHolder.getCurrentResidenceId());
	    	}
	    	request.setAttribute("companey",companyService.getComboboxs(company));
	    	//性别
	    	request.setAttribute("sexType", SexEnum.values());
	    	//证件种类
	    	request.setAttribute("credentialType", CredentialType.values());
	    	return "/system/propertystaffmanager/editPropertyEmployee"; 
	    }
	    /**
	     * @title 分配物业公司
	     * @createTime 2015年9月26日 下午5:06:44
	     * @author dhc
	     */
	    @RequestMapping("dispatchPropertyPage")
	    public String dispatchPropertyPage(HttpServletRequest request,Long id){
	    	ShPropertyCompany company = new ShPropertyCompany();
	    	if(!UserHolder.isSuperResidence()){
	    		company.setResidenceId(UserHolder.getCurrentResidenceId());
	    	}
	    	request.setAttribute("companey",companyService.getComboboxs(company));
	    	request.setAttribute("employee", employeeService.queryPropertyEmployeeById(id));
	    	//性别
	    	request.setAttribute("sexType", SexEnum.values());
	    	//证件种类
	    	request.setAttribute("credentialType", CredentialType.values());
	    	
	    	return "/system/propertystaffmanager/dispatchPropertyPage"; 
	    }
	    
	    
	    
	    @ResponseBody
	    @RequestMapping("/add")
	    public Result add(HttpServletRequest request, ShPropertyEmployee employee){
	    	//验证
	    	if(StringUtils.isBlank(employee.getName())){
	    		return Result.failure("名字不能为空");
	    	}
	    	
	    	if(null == employee.getCompanyId()){
	    		return Result.failure("请选择一个物业公司");
	    	}
	    	
	    	//重名验证
	    	if(employeeService.isReapet(employee)){
	    		return Result.failure("名字出现重复");
	    	}
	    	
	    	employeeService.add(employee);	    	
	    	return Result.success("添加成功");
	    }
	    
	    @ResponseBody
	    @RequestMapping("/delete")
	    public Result delete(HttpServletRequest request,Long id){
	    	try {
	    		int count = employeeService.delete(id);
	    		if(count <= 0){
	    			return Result.failure("删除数据失败");
	    		} 
	    	} catch(RuntimeException e){
	    		logger.info(e.getMessage());
	    		return Result.failure(e.getMessage());
	    	} catch(Exception e){
	    		logger.info(e.getMessage());
	    		return Result.failure("删除数据失败");
	    	}
	    	
	    	
	    	return Result.success("删除数据成功");
	    }
	    
	    @ResponseBody
	    @RequestMapping("/edit")
	    public Result edit(HttpServletRequest request, ShPropertyEmployee employee){
	    	//验证
	    	if(StringUtils.isBlank(employee.getName())){
	    		return Result.failure("名字不能为空");
	    	}
	    	
	    	if(null == employee.getCompanyId()){
	    		return Result.failure("请选择一个物业公司");
	    	}
	    	
	    	//重名验证
	    	if(employeeService.isReapet(employee)){
	    		return Result.failure("名字出现重复");
	    	}
	    	
	    	employeeService.eidt(employee);   	
	    	return Result.success("编辑成功");
	    }
	    
	    @ResponseBody
	    @RequestMapping("/dispatchProperty")
	    public Result dispatchProperty(HttpServletRequest request, ShPropertyEmployee employee){
	    	Long id = employee.getId();
	    	
	    	if(null == employee.getCompanyId()){
	    		return Result.failure("请选择一个物业公司");
	    	}
	    	
	    	ShPropertyEmployee se = employeeService.queryPropertyEmployeeById(id);
	    	if(null == se){
	    		return Result.failure("未查询用户，指定物业公司失败");
	    	}
	    	
	    	
	    	se.setCompanyId(employee.getCompanyId());
	    	employeeService.eidt(se);	    	
	    	return Result.success("指定物业公司成功");
	    }
	    
}
